
package com.android.matmanual;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.AudioManager;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.util.Log;


public class FMTest extends BaseActivity{
	TextView tv;
	TextView btntv;
	LinearLayout btnOn;
	HeadsetPlugReceiver receiver;
	IntentFilter mIntentFilter;
	Boolean bool = false;
	Boolean fmstart = false;
	AudioManager mAudioManager;
	WakeLock mWakeLock;	
	
	
	public void stopFM()
	{
		
		Intent tent = new Intent();
		tent.setClassName("com.thunderst.radio", "com.thunderst.radio.RadioService");
		stopService(tent);
	}
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE); 
		mWakeLock = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "XYTEST"); 
		mWakeLock.acquire();
		
		setContentView(R.layout.fmtest);
		Log.d("FMTest", "################### setContentView");
		setTitle(R.string.fmtest_name);
	    
		tv = (TextView)findViewById(R.id.tv);
		btnOn = (LinearLayout)findViewById(R.id.btnOn);
		btntv = (TextView)btnOn.findViewById(R.id.textView);
		Log.d("FMTest", "################### setContentView 1");
		mIntentFilter = new IntentFilter();
		receiver = new HeadsetPlugReceiver();
		mIntentFilter.addAction(Intent.ACTION_HEADSET_PLUG);
		registerReceiver(receiver, mIntentFilter); 
		tv.setText(R.string.fmclose);
		Log.d("FMTest", "################### setContentView 2");
		mAudioManager = (AudioManager) getSystemService(AUDIO_SERVICE);		
	//	mAudioManager.setMode(0);
		while(mAudioManager.getMode() != 0)
		{
			Log.v("lifei-MatManual", "##################### audioManager.getMode try to set FM");
			mAudioManager.setMode(0);
		}
		Log.d("lifei", "################### audioManager.getMode():"+mAudioManager.getMode());
		bool = mAudioManager.isWiredHeadsetOn();
		btnOn.setOnClickListener(new OnClickListener() {
		
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if(bool)
				{
					Intent tent = new Intent();
					tent.setComponent(new ComponentName("com.mediatek.FMRadio","com.mediatek.FMRadio.FMRadioActivity"));
					FMTest.this.startActivity(tent);
					fmstart = true;
				}
				else
				{
					tv.setText(R.string.insertheadset);
				}
			}
		});
	}
	
	public class HeadsetPlugReceiver extends BroadcastReceiver{
		@Override
		public void onReceive(Context arg0, Intent arg1) {
			// TODO Auto-generated method stub
			 if(arg1.getIntExtra("state", 0) == 1){
				 bool = true;
			 }
			 else
			 {
				 bool = false;
			 }
		}
	}
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		
		super.onPause();
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		int result = TestControl.GetKeyEvent(keyCode);
		if (result != TestControl.DO_NOTHING) {
			setResult(result);
			finish();
	    }
	    return true;
	}
	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		if(fmstart==true){
			stopFM();
		}
	//	mAudioManager.setMode(AudioManager.MODE_RINGTONE);
		super.onStop();
	}
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		
		unregisterReceiver(receiver);
		if(mWakeLock!=null)
		{
			mWakeLock.release(); 
			mWakeLock = null;
		}
		super.onDestroy();
	}
	
	@Override
	public void onAttachedToWindow() 
	{
		// TODO Auto-generated method stub
		super.onAttachedToWindow();
		this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
	}


}
		 